package demo4;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-07-02
 * Time: 20:48
 */

class Student implements Comparable<Student> {
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
/*        if(this.age - o.age > 0){
            return 1;
        }else if(this.age - o.age == 0){
            return 0;
        }else{
            return -1;
        }
    }*/
        return o.age - this.age;
    }
}
public class Test {
    public static void main3(String[] args) {
        Student student1 = new Student("张三",19,28.5);
        Student student2 = new Student("李四",18,45.8);
        int ret = student1.compareTo(student2);
        System.out.println(ret);
    }


    public static void main4(String[] args) {
        Student[] arry = new Student[3];
        arry[0] = new Student("张三",19,28.5);
        arry[1] = new Student("李四",18,45.8);
        arry[2] = new Student("王五",21,65.3);
        System.out.println("排序前"+Arrays.toString(arry));
        bubbleSort(arry);
        System.out.println(Arrays.toString(arry));
    }

    public static void bubbleSort(Comparable[]  comparables){
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }

    public static void main1(String[] args) {
        int[] arry = {1,3,2,9,4,8};
        Arrays.sort(arry);
        System.out.println("排序后"+Arrays.toString(arry));
    }
}
